package net.zjitc.summary.student;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @Author admin
 * @Date 2019/5/5 7:52
 */

public class Test {
    static Scanner scanner = new Scanner(System.in);
    static Scanner scannerInt = new Scanner(System.in);
    static StudentDao dao = new StudentDaoDB();

    public static void main(String[] args) {
        while (true) {
            System.out.println("欢迎使用学籍管理系统!");
            System.out.println("1--查询学生名单");
            System.out.println("2--添加新学生信息");
            System.out.println("3--更新学生信息");
            System.out.println("4--删除学生信息");
            System.out.println("5--退出系统");
            System.out.println("请输入你的选择（1-5）：");
            int choose = scannerInt.nextInt();
            switch (choose) {
                case 1:
                    selectUI();
                    break;
                case 2:
                    insertUI();
                    break;
                case 3:
                    updateUI();
                    break;
                case 4:
                    deleteUI();
                    break;
                case 5:
                    System.out.println("欢迎下次光临");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误，请输入1-5：");
            }
        }
    }

    private static void insertUI() {
        System.out.println("-----添加界面------");
        System.out.print("请输入学生学号：");
        String id = scanner.nextLine();
        System.out.print("请输入姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入性别：");
        String sex = scanner.nextLine();
        System.out.print("请输入年龄：");
        int age = scannerInt.nextInt();
        System.out.print("请输入班级：");
        String className = scanner.nextLine();
        Student student = new Student(id, name, sex, age, className);
        /* 调用工具类的添加方法，将student对象保存在数据库*/
        Student s = dao.selectByID(id);
        if (s == null) {
            int c = dao.insert(student);
            if (c > 0) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败！");
            }
        } else {
            System.out.println("添加失败!该学号重复了");
        }

    }

    private static void selectUI() {
        System.out.println("-----所有学生信息如下：-------");
        System.out.println("\t学号\t姓名\t性别\t年龄\t班级");
        /* 查询返回，返回保存图书对象的集合*/
        ArrayList<Student> arrayList = dao.selectAll();
        /*遍历集合，显示*/
        Iterator<Student> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getSex() + "\t" + student.getAge() + "\t" + student.getClassName());
        }
    }

    private static void updateUI() {
        System.out.println("-----更新界面-----");
        System.out.println("请输入要修改的学生学号：");
        String id = scanner.nextLine();
        Student s = dao.selectByID(id);
        if (s == null) {
            System.out.println("你要更新的学生不存在");
        } else {
            System.out.println("\t学号\t姓名\t性别\t年龄\t班级");
            System.out.println(s.getId() + "\t" + s.getName()
                    + "\t" + s.getSex() + "\t" + s.getAge() + "\t"
                    + s.getClassName());
            System.out.print("请输入姓名：");
            String name = scanner.nextLine();
            System.out.print("请输入性别：");
            String sex = scanner.nextLine();
            System.out.print("请输入年龄：");
            int age = scannerInt.nextInt();
            System.out.print("请输入班级：");
            String className = scanner.nextLine();
            Student student = new Student(id, name, sex, age, className);
            int c = dao.update(student);
            if (c > 0) {
                System.out.println("更新成功！");
            } else {
                System.out.println("更新失败！");
            }
        }
    }

    private static void deleteUI() {
        System.out.println("-----删除界面-----");
        System.out.println("请输入要删除的学生学号：");
        String id = scanner.nextLine();
        Student student = dao.selectByID(id);
        if (student == null) {
            System.out.println("要删除的学生不存在");
        } else {
            int c = dao.delete(id);
            if (c > 0) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败！");
            }
        }

    }
}

